<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {

    public function indexAction(){
        $model = M();
        $sql = "select t1.*,t2.group_name from user t1 left join grouplist t2 on t1.gid =t2.id";
        $useInfo = $model->query($sql);
        $this->assign('useInfo',$useInfo);
        $nowTime = time();
        $this->assign('nowTime',$nowTime);


        $this->display('index');
    }

    public function addAction(){
        $this->display('add');
    }

    /**
     * 编辑用户
     */
    public function editAction(){
        $id = I('get.id');
        $model = M();
        $sql = "select * from user where id ='{$id}'";
        $userInfo = $model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);

        $sql = "select * from jobs";
        $jobsInfo = $model->query($sql);
        $this->assign('jobsInfo',$jobsInfo);

        $sql = "select * from grouplist";
        $groupInfo = $model->query($sql);
        $this->assign('groupInfo',$groupInfo);

        $this->display('edit');
    }

    /**
     * 更新用户资料
     */
    public function updateAction(){
        $postData = I('post.');
        $model = M();
        if($postData['group_name'] =='管理员') {
            $gid = 1;
        }elseif($postData['group_name'] =='会员'){
            $gid = 2;
        }else{
            $gid = 3;
        }

        $sql= "update user set gid='{$gid}',job='{$postData['job']}',sex='{$postData['sex']}',username='{$postData['user_name']}',province='{$postData['province']}',introduce='{$postData['signature']}',qq='{$postData['qq']}',phone='{$postData['mobile']}',mail='{$postData['email']}',site='{$postData['homepage']}' where id='{$postData['id']}' ";
        $model->execute($sql);
        if(!empty($postData['password'])){
            $newPassword = md5($postData['password']);
            $sql= "update user set password='{$newPassword}' where id='{$postData['id']}' ";
            $model->execute($sql);
        }
        $this->success('修改成功','');

    }

     /**
      * 封禁用户
      */
     public function closeAction(){
         $id = I('get.id');
         $model = M();
         $sql = "update user set gid=3 where id='{$id}'";
         if($model->execute($sql)){
             $this->success('封禁用户成功',U('Admin/User/index'));
         }

     }
}